﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>客户管理</title>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/jquery/jquery.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/plupload/plupload.full.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/jquery/jquery-ui/jquery-ui.js"></script>
<script
	src="${pageContext.request.contextPath}/js/bootstrap/bootstrap.js"
	type="text/javascript"></script>
<script
	src="${pageContext.request.contextPath}/ui/table/bootstrap-table.js"
	type="text/javascript"></script>
<script
	src="${pageContext.request.contextPath}/ui/table/locale/bootstrap-table-zh-CN.js"
	type="text/javascript"></script>
<script
	src="${pageContext.request.contextPath}/ui/table/plugins/tableExport.js"
	type="text/javascript"></script>
<script
	src="${pageContext.request.contextPath}/ui/table/plugins/table-export.js"
	type="text/javascript"></script>
<script
	src="${pageContext.request.contextPath}/ui/table/plugins/jquery.base64.js"
	type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/ui/layui/layui.js"
	type="text/javascript"></script>
<script
	src="${pageContext.request.contextPath}/ui/layui/lay/modules/laydate.js"
	type="text/javascript"></script>
<script
	src="${pageContext.request.contextPath}/ui/layui/lay/modules/layer.js"
	type="text/javascript"></script>
<script
	src="${pageContext.request.contextPath}/ui/layui/lay/modules/form.js"
	type="text/javascript"></script>
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/ui/layui/css/modules/laydate/laydate.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/jquery-css/jquery-ui.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/bootstrap-css/bootstrap.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/ui/table/bootstrap-table.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/ui/layui/css/layui.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/ui/layui/css/global.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/ui/layui/css/modules/layer/default/layer.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/ui/layui/css/modules/code.css">


<style>
.container {
	width: 85%;
}

.modal-company {
	width: 700px;
}

.modal-body {
	max-height: 750px
}

.modal-body-company {
	height: 600px;
}

.modal-account {
	width: 400px;
	z-index: 2000;
	left: 65%;
}

.modal-account.fade.in {
	top: 15%;
}

.modal-body-account {
	height: 200px;
}
</style>


</head>

<body>
	<%@include file="/webpage/frame/top.jsp"%>
	<div class="container">
		<div id="progressbar"></div>
		<div id="finish"
			style="font-weight: bold; font-size: 20px; margin: 10px auto">
			<button id="uploader" style="height:40px;width:150px;margin:  10px auto " >上传备份文件</button>
			&nbsp;&nbsp;请选择要上传的beb文件
		</div>

		<div id="modal" class="modal-company modal fade" style="display: none " >
			<div class="modal-dialog" >
				<div class="modal-content" >
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal"
							aria-label="Close">
							<span aria-hidden="true">&times;</span>
						</button>

						<span class="modal-title"></span><span class="alert-company"></span>
					</div>
					<div class="modal-body-company modal-body">
						<div class="form-group" >
                    	 <label>文件ID</label>
                    	 <input type="text" class="form-control"
								name="backupId" id="backupId" placeholder="文件Id" readonly="readonly"
								style="width: 400px">
                    	</div>
						<div class="form-group">
							<label>公司名称</label> <input type="text" class="form-control"
								name="companyName" placeholder="公司名称" readonly="readonly"
								style="width: 400px">
						</div>
						<div class="form-group">
                    	 <label>文件名</label>
                    	 <select name="descriptionId" id="description"  lay-verify="required" lay-search="required" >
                    	 </select>
                    	</div>
                    	<div class="form-group">
                    	 <label>标签</label>
                    	   
                    	   <select name="isGood" id="isGood"  lay-verify="required" lay-search="required" readonly="readonly">
                    	 
                    	 
                    	 </select>	
                    	 
                    	 
                    	 
                    	</div>
                    		<button type="button" class="signGood btn btn btn-default " onclick="signupGoodBackup();" >标记优秀</button>
                    	<button type="button" class="signCommon btn btn btn-default " onclick="signupCommomBackup();" >标记普通</button>
                    	<button type="button" class="remove btn btn btn-default" onclick="removeBack();">删除备份</button>
                    	<button type="button" class="download btn btn btn-default">下载备份</button>
					</div>
					
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
						<button type="button" class="btn btn-primary submit">提交公司资料</button>
					</div>
					
				</div>
			</div>
		</div>
		<div class="layui-form-pane" style="margin-top: 15px;"></div>




		<table id="companytable" data-show-refresh="true" data-search="true"
			data-click-to-select="true" data-show-export="true"
			data-query-params="queryParams" data-pagination="true"
			data-toolbar=".toolbar">
			<!-- data-show-columns="true"  -->
			<thead>
				<tr>
					<th data-field="state" data-radio="true"></th>
					<th data-field="id" data-show="true" data-visible="false">id</th>
					<th data-field="companyName" data-sortable="true">客户公司名称</th>

					<!-- <th data-field="registerTime">注册时间</th> -->
					<th data-field="username" data-sortable="true">负责客服</th>
					<th data-field="industry" data-sortable="true">所属行业</th>
					
					<th data-field="hasPath" data-show="true" data-visible="false">地址</th>
					<th data-field="action" data-align="center"
						data-formatter="actionFormatter" data-events="actionEvents">操作</th>

				</tr>
			</thead>

		</table>





	</div>


	<!-- 上传组件初始化代码 -->
	<script type="text/javascript">
		//上传公司筛选

		 var companyid="";
 		var industryId= '';
 		var accountId = '';
 		var interval_import = '';
 		var upload_click = false;
        var uploader;
        
       
        upload();
        uploader.init();
        
      function getImportMsg(){
    	  var GET_IMPORT_MSG = "${pageContext.request.contextPath}/importStateController.do?getImportStateMsg";
    	  $.ajax({  
  	       	 url: GET_IMPORT_MSG  
  	        , type: "POST"
  	        
  	        , data: {  
  	        	companyId:companyId
  	        }, 
  	        dataType :"json" ,
  	        beforeSend: function(request){  
  	        }, success: function(data, status){
  	        	
  	        		//console.log("返回信息："+data);
  	        		if(data=='0'||data==''){
  	        			$("#finish").html('网络失败');
  	        			$("#finish").append("&nbsp;&nbsp<button id='uploader' style='height:40px;width:150px;margin:  10px auto ' >重新上传beb</button>");
  	        			clearInterval(interval_import);
  	        			upload();
  	        			uploader.init();
  	        		}
  	        		
  	        	
  	        		if(isContains(data,'导入成功')){
  	        			$("#finish").html(data);
  	        			$("#finish").append("&nbsp;&nbsp<button id='uploader' style='height:40px;width:150px;margin:  10px auto ' >再次上传beb</button>");
  	        			clearInterval(interval_import);
  	        			upload();
  	        			uploader.init();
  	        			listenUploadButton();
  	        		}else if(isContains(data,'导入失败')){
  	        			$("#finish").html(data);
  	        			$("#finish").append("&nbsp;&nbsp<button id='uploader' style='height:40px;width:150px;margin:  10px auto ' >重新上传beb</button>");
  	        			clearInterval(interval_import);
  	        			upload();
  	        			uploader.init();
  	        			listenUploadButton();
  	        			//uploader.init();
  	        		}else{
  	        			$("#finish").html(data);
  	        			
  	        		}
  	        		
  	        	
  	        		
  	        		
  	        }, error: function(request, error, status){  
  	            // todo: 错误处理  
  	            
  	        }, complete: function(request, error, status){  
  	        	
  	        }  
  	    });  
    	  
      }
      
      function upload(){
    	  uploader = new plupload.Uploader({
    		  runtimes : 'html5,flash,silverlight,html4',//设置运行环境，会按设置的顺序，可以选择的值有html5,gears,flash,silverlight,browserplus,html
              flash_swf_url : './js/plupload/Moxie.swf',
              silverlight_xap_url : './js/plupload/Moxie.xap',
              url : '${pageContext.request.contextPath}/uploadBebFile.do?reportType=searchReport&companyId='+companyid,//上传文件路径
              max_file_size : '50mb',//100b, 10kb, 10mb, 1gb
              chunk_size : '50mb',//分块大小，小于这个大小的不分块
              unique_names : true,//生成唯一文件名
              browse_button : 'uploader', 
              filters: [{title: "Custom Files",extensions: "beb"}],//图片限制
             
              init : {
            	 
              	FileFiltered:function(up,file){
              		$("#finish").html("请选择正确的文件类型")
              	},
                  FilesAdded: function(up, files) {
                	 
                      uploader.start();
                      $("#finish").html("开始上传")
                     
                  },
                  FileUploaded : function(up, file, info) {//文件上传完毕触发
                	  
                  	 var response = $.parseJSON(info.response);
                     
                    
                      if (response.status) {
                      	 $("#finish").html(response.message);
                          //$('#result').append( $('<div> "文件路径是："' + response.fileUrl + '"随机的文件名字为："' + file.name + '</div>') );
                      }else{
                      	 $("#finish").html(response.message);
                      }
                  },
                  UploadComplete : function( uploader,files ) {
                	  
                	    uploader.destroy();
                  		interval_import = setInterval("getImportMsg()",50);
                  		
                  		listenUploadButton();
                     		
                  },
                  UploadProgress : function( uploader,file ) {
                     // $("#progress").html("上传进度为：" + file.percent + "%");
                     
                     $( "#progressbar" ).progressbar({
        					value:  file.percent
        			 });
                     
                  },
                  Error: function(up, args) {
                      // Called when error occurs
                      if(args.code==-601){
                    	  $("#finish").html("<font color='red'>只允许上传beb文件</font>&nbsp;&nbsp<button id='uploader' style='height:40px;width:150px;margin:  10px auto ' >再次上传beb</button>")	  
                    	  upload();
                    	  uploader.init();
                    	  listenUploadButton();
                      
                      }
                     
                  }
              }
          });
    	  
    	 
    	  
    
      }
      
      
      
      function isContains(str, substr) {
    	    return str.indexOf(substr) >= 0;
    	}
      
      function getAccount(){
    	  
    	  //console.log('getAccount');
    	  var obj = eval($table.bootstrapTable('getSelections'));
    	 console.log(obj);
    	  companyId=obj[0].id;
    	  industryId = obj[0].industryId;
    	 // console.log(companyId)
    	  accountId= '0';
    	  var GET_ACCOUNT_URL = '${pageContext.request.contextPath}/companyController.do?checkAccount'
    	  $.ajax({  
    	        url: GET_ACCOUNT_URL  
    	        , type: "POST"
    	        , async:true
    	        , data: {  
    	        	companyId:companyId
    	        }, 
    	        dataType :"json" ,
    	        beforeSend: function(request){  
    	        }, success: function(data, status){
    	        	
    	        	
    	        	accountId= data;
    	        	
    	        	if(accountId!='0'){
    	        		console.log($(".moxie-shim-html5").find('input[type=file]'));
    	        	
        	        	uploader.settings.url = '${pageContext.request.contextPath}/uploadBebFile.do?reportType=searchReport&accountId='+accountId+'&industryId='+industryId+'&companyId='+companyId
    	        		
    	        	}else{
    	        		alert('此公司面没有添加百度账号');
    	        		return false;
    	        	}
    	        		return true;
    	        		
    	        }, error: function(request, error, status){  
    	            // todo: 错误处理  
    	        }, complete: function(request, error, status){  
    	        }  
    	    });  
    	  
    	 	return false;
      }
      
      $(
    	
    	function(){
    		$("#uploader").mouseover(
    				
    			function(){
    				
    				if($(".bs-checkbox input:radio[name='btSelectItem']:checked").val()==undefined){
    					alert("请选择一间公司");
    				    //uploader.destroy();	
    					return  false;
    				}else{
    					
    					getAccount();
    			 	}
    			}
    		);
    		 
    	}
    	
    	
    	
    	
      );
      function listenUploadButton(){
  		$("#uploader").mouseover(
  				
      			function(){
      				//console.log('aaa');
      				if($(".bs-checkbox input:radio[name='btSelectItem']:checked").val()==undefined){
      					alert("请选择一间公司");
      				    //uploader.destroy();	
      					return  false;
      				}else{
      					
      					getAccount();
      			 	}
      			}
      		);
  	}
	</script>

	<script>
	

		$modal = $('#modal').modal({
			show : false
		}), $alert = $('.alert').hide();
		var API_URL = '${pageContext.request.contextPath}/companyController.do?getCompanysAndIsGood';

		//获取加载公司列表
		var $table = $('#companytable').bootstrapTable({
			url : API_URL,
			pageSize : 25,
			pageList : [ 15, 25, 100, 500 ]
		}),

		$alert = $('.alert').hide();

		$(function() {

			//拼接json数据

			//var  accountPlatformName = $("#typeTag").find("option:selected").text();
			//$("#accountPlatformName").val(
			//		accountPlatformName
			//);

			$(function() {
				//获取选中值

				TableExport.init();
				//导出按钮
				$('#export').click(function() {

					$table.tableExport({
						type : 'excel',
						escape : false
					});
				});
				// create event

			});
		});

		function queryAccountParams(params) { //配置参数  

			var temp = { //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的  
				companyId : $modal.data("id")

			};
			return temp;
		}

		function queryParams(params) {
			return {};
		}

		function actionFormatter(value, row, index) {

			buttonArray = new Array();

			//&companyName="+"\""+name+"\""+"\""
			var id = row.id;

			//var str = "<a class=\"download-backup btn btn-default\" href=\"${pageContext.request.contextPath}/companyController.do?downloadBackup&id="
				//	+ id + "\"" + " >下载备份</a>"
					
			if (row.hasPath ) {
				buttonArray[0] = '<a class="detail btn btn btn-default" href="javascript:" title="详情">详情</a>';
				
			}

			return buttonArray.join(' ');
		}

		// update and delete events
		window.actionEvents = {

				
				
				
			'click .remove-backup' : function(e, value, row) {

				if (confirm('你确定要删除此公司备份?')) {
					$
							.ajax({
								url : '${pageContext.request.contextPath}/companyController.do?deleteAllBackup',
								type : 'post',
								data : {
									id : row.id
								},
								success : function() {
									$table.bootstrapTable('refresh');
									showAlert('删除备份!', 'success');
								},
								error : function() {
									showAlert('删除失败!', 'danger');
								}
							})

				}
			},

			

			
			'click .detail' : function(e, value, row){
				    		
				    		
				    	$(location).attr('href','${pageContext.request.contextPath}/companyController.do?getBackupDescription&id='+row.id);
				    		
				    
				    
				    },
				    /*
				alert(row.id);
				
				$.post("${pageContext.request.contextPath}/companyController.do?getBackupDescription",
						{id:row.id},
						
						function(result){
					 
							var data = $.parseJSON(result);
					      
					      
							
					       for(i=0;i<data.length;i++){
					    	   $("#isGood").append("<option value="+data[i]["isGood"]+" >"+data[i]["isGood"]+"</option>");
					    	  $("#description").append("<option value="+data[i]["backupId"]+" >"+data[i]["description"]+"</option>");
					    	  
					       
					       }
					       */
					    	
						
						
					
				//showModal($(this).attr('title'), row);
		
						};
		
		//点击详情调用
		function showModal(title, row) {

			for ( var name in row) {
				
				$modal.find('input[name="' + name + '"]').val(row[name]);
				  
			}
			
			$modal.modal('show');
			
			

		};
		$("#description").change(function(){
			var description = $('#description').val();
			
			$("#backupId").val(description);
			
		});
		
		
		function removeBackup(){
			var backupId = $("#description").val();
			$.post('${pageContext.request.contextPath}/companyController.do?deleteBackup',
			{id:backupId},
			function(){
				alert("删除成功");
			}
			);
		};
		
		function signupGoodBackup(){
			var backupId = $("#description").val();
			
			$.post('${pageContext.request.contextPath}/companyController.do?signBackup',
			{id:backupId},
			function(){
				alert("标记成功");
			}
			);
		};
		
		
	</script>


	<%@include file="/webpage/frame/bottom.jsp"%>
</body>
</html>